<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Day04 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/myhugo/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0d ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/myhugo/"><h1>我的博客</h1></a>
      <p class="lead">
       小白的逆袭之旅 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/myhugo/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Day04</h1>
  <time datetime=2020-04-25T22:19:36&#43;0800 class="post-date">Sat, Apr 25, 2020</time>
  <h3 id="封装api">封装api</h3>
<pre><code>//文件名：http.js

import axios from 'axios'
axios.defaults.baseURL=&quot;http://127.0.0.1:8000&quot;
axios.defaults.timeout=1000 *10

axios.defaults.headers.post['Content-Type']='application/json';
axios.defaults.headers.put['Content-Type']='application/json';
//封装get请求
export function axios_get(url,params){
    return new Promise(
        (resolve,reject)=&gt;{
            axios.get(url,{params:params})
            .then(res=&gt;{
                resolve(res.data)
            }).catch(err=&gt;{
                reject(err.data)
            })
        }
    )
}

//封装post请求
export function axios_post(url,data){
    return new Promise(
        (resolve,reject)=&gt;{
            console.log(data)
            axios.post(url,JSON.stringify(data))   //将json数据编译成字符串
            .then(res=&gt;{
                resolve(res.data)
            }).catch(err=&gt;{
                reject(err.data)
            })
        }
    )
}

//封装put请求
export function axios_put(url,data){
    return new Promise(
        (resolve,reject)=&gt;{
            console.log(data)
            axios.put(url,JSON.stringify(data)) 
            .then(res=&gt;{
                resolve(res.data)
            }).catch(err=&gt;{
                reject(err.data)
            })
        }
    )
}


封装delete请求
export function axios_delete(url,data){
    return new Promise(
        (resolve,reject)=&gt;{
            console.log(data)
            axios.delete(url,{params:data})
            .then(res=&gt;{
                console.log(&quot;封装信息的的res&quot;,res)
                resolve(res.data)
            }).catch(err=&gt;{
                reject(err.data)
            })
        }
    )
}
</code></pre><h3 id="导出">导出</h3>
<pre><code>
import {axios_get,axios_post,axios_put,axios_delete} from '../axios_send/http'
export const api_get= p =&gt; axios_get('/app/index01/',p)</code></pre>
</div>


    </main>

    
  </body>
</html>
